let global = getApp().globalData;
let util = global.util;
let wxApi = global.wxApi;
let Api = global.Api;

Page({
  data: {
    baseWidth: 220,
    areaWidth: 375,
    baseY: 30,
    zIndexes: [{
      zIndex: 0,
      active: false
    }, {
      zIndex: 1,
      active: false
    }, {
      zIndex: 2,
      active: false
    }, {
      zIndex: 3,
      active: false
    }],
    cartCodes: [],
    time: '00:00',
    showGuidance: false
  },
  onShow() {
    this.init();
  },
  onLoad(options) {
    this.type = options.type;
    wxApi.setNavTitle('开始练习')
  },
  
  init() {
    if ( this.backVal ) return;
    this.isMove = false;
    this.isViewed = false;
    this.loadCodes();
    this.codes = [];
    this.trainedCodes = {
      keys: [],
      codes: [],
      forgetNum: 0
    }
    this.guidanceImp();
    wxApi.getScreenSize(size => {
      this.setData({
        areaWidth: size.width
      })
    })
  },
  back() {
    // this.clearTimer();
    this.backVal = true;
    wx.navigateBack({
      delta: 1
    })
  },
  /**
   * 卡片点击事件
   */
  onCartTap( e ) {
    let dataset = e.currentTarget.dataset;
    // 当前滑动的卡片下标
    let touchIndex = dataset.index;
    // 当前滑动的卡片高度
    let zIndex = dataset.zindex;
    let zIndexes = this.data.zIndexes;
    // 根据已有标识actice，采取相反翻转行为
    let active = zIndexes[touchIndex].active;
    zIndexes[touchIndex].active = !active;
    this.setData({
      zIndexes: zIndexes
    })
    this.isViewed = true;
  },
  /**
   * 卡片移动事件
   */
  onCartMove(e) {
    let dataset = e.currentTarget.dataset;
    // 当前滑动的卡片下标
    let touchIndex = dataset.index;
    // 当前滑动的卡片高度
    let zIndex = dataset.zindex;
    // 如果是触摸滑动，则标记为滑动事件
    this.isMove = true;
    
  },
  /**
   * 触摸移动结束事件
   */
  onTouchEnd( e ) {
    console.log( 'touch end' )
    if ( !this.isMove ) {
      return;
    } 
    this.isMove = false;
    let dataset = e.currentTarget.dataset;
    // 当前滑动的卡片下标
    let touchIndex = dataset.index;
    // 当前滑动的卡片高度
    let zIndex = dataset.zindex;
    let code = dataset.code;
    let zIndexes = this.data.zIndexes;
    console.log('touch-index: ' + touchIndex + ', z-index: ' + zIndex)
    if (zIndex < 3) {
      this.setData({
        zIndexes: zIndexes
      })
      return;
    }
    console.log( code );
    this.addCode( code );
    for (let i = 0, len = zIndexes.length; i < len; i ++) {
      if ( i == touchIndex ) {
        zIndexes[ i ].zIndex = zIndexes[ i ].zIndex - 3;
      } else {
        zIndexes[ i ].zIndex = zIndexes[ i ].zIndex + 1;
      }
      // 取消卡片答案展示效果
      zIndexes[ i ].active = false;
    }
    this.setData({
      zIndexes: zIndexes
    })
    if (touchIndex == 0) {
      this.initCartCodes();
      
    }
    
  },
  /**
   * 引导图处理逻辑
   */
  guidanceImp() {
    global.store.getGuidTimeByKey('ynxl', num => {
      console.log( num );
      let showGuidance = false;
      if ( num === 1 ) {
        showGuidance = true
      } else {
        showGuidance = false
      }
      this.setData({
        showGuidance: showGuidance
      })
    })
  },
  closeGuidance() {
    this.setData({
      showGuidance: false
    })
  },
  /**
   * 结束练习
   */
  endTraining() {
    wxApi.navigateTo('../training-account/training-account')
  },
  loadCodes() {
    Api.traningCodesList(this.type, res => {
      this.codes = res;
      let initCodes = this.initCartCodes();
      // this.addCode( initCodes[ initCodes.length - 1 ] )
    })
  },
  initCartCodes() {

    let len = this.codes.length;
    let cartCodes = [];
    for (let i = 0; i < 4; i++) {
      let index = util.randomNum( 0, len - 1 );
      if ( !this.codes[ index ] ) {
        i --;
        continue;
      }
      cartCodes.push(this.codes[ index ]);
    }
    console.log(cartCodes);
    this.setData({
      cartCodes: cartCodes
    })
    return cartCodes;
  },
  /**
   * 秒表计时器
   *
   */
  countdownClock() {
    let self = this;
    clearInterval(this.timer);
    let n = 0;
    this.timer = setInterval(function () {
        n ++;
        var m = parseInt( n / 60 );
        var s = parseInt( n % 60 );
        let value = toDub( m ) + ":" + toDub( s );

    }, 1000/60);
  },
  addCode(code) {
    let trainedCodes = this.trainedCodes;
    let codeKey = code.key;
    if ( this.isViewed ) {
      trainedCodes.forgetNum = trainedCodes.forgetNum + 1;
      this.isViewed = false;
      code.viewed = true;
    } else {
      code.viewed = false;
    }
    // 判断是否已存在
    let existIndex = trainedCodes.keys.indexOf( codeKey );
    // if ( existIndex === -1 ) {
    //   trainedCodes.keys.push( codeKey );
    //   trainedCodes.codes.push( code );
    // } else {
    //   if ( code.viewed ) {
    //     trainedCodes.codes[existIndex] = code;
    //   }
    // }
    trainedCodes.keys.push( codeKey );
    trainedCodes.codes.push( code );
    console.log( this.trainedCodes );
    this.trainedCodes = trainedCodes;
    return trainedCodes;
  }
})
// 补零
function toDub(n){
  return n<10?"0"+n:""+n;
}